<?php

include "CryptAES.php";

class Pay{

    public $orgNo = '20171102001';
    public $keyStr = 'eXVubHUua2V50000';
    public $url = 'http://trx.jinqianglv.com/trx/bankPay';
    public function toPay(){
        $post = array();
        $plainText = $this->getContext();
        $post['signData'] = $this->getSignData( $plainText );
        $post['encrtpKey'] = $this->getEncryptKey();
        $post['orgNo']  = $this->orgNo;


        $aes = new CryptAES();
        $aes->set_key($this->keyStr);
        $aes->require_pkcs5();
        $post['context'] = $aes->encrypt($plainText);

        $ret = $this->curlPost($post);
        $returns = json_decode($ret,true);

        echo "-------or begain--------"."\n";
        echo $ret."\n";
        echo "-------or end--------"."\n";

        if( $returns['context'] ){
            $this->payBack( $returns, $post['signData'] );
        }else{
            print_r($returns);exit;
        }

    }

    public function payBack( $paybackdata, $signData ){
        //解密encrtpKey
        $private_key_yunlu = file_get_contents("rsa_private_key_yunlu.pem");
        $pi_key = openssl_pkey_get_private($private_key_yunlu);
        openssl_private_decrypt(base64_decode($paybackdata['encrtpKey']),$keyStr,$pi_key);
        openssl_free_key($pi_key);
        echo $keyStr."\n";

        $aes = new CryptAES();
        $aes->set_key($keyStr);

        $decString = $aes->decrypt($paybackdata['context']);
        print_r($decString)."\n";
        $decString = htmlspecialchars_decode($decString);
        $context_array = json_decode($decString);
        var_dump($context_array);exit;
        //签名验证
        $public_key_yunlu = file_get_contents("rsa_public_key_yunlu.pem");
        $pu_key = openssl_pkey_get_public($public_key_yunlu);
        $verify = openssl_verify($decString, base64_decode($signData), $pu_key, OPENSSL_ALGO_SHA1);
        openssl_free_key($pu_key);


    }

    public function getContext(){
        $orderNumber = "0000000".rand(100,999);
        $param = array(
            //'merchantCode' => '20180104135900035',
            'merchantCode' => '20180104135900035',
            'orderNumber' => $orderNumber,
            'apiorderNumber' => '',
            'tranCode' => '051',
            'totalAmount' => "100",
            'payType' => 5,
            'subject' => '测试',
            'bankId' => '',
            'cardType' => '',
            'callback' => 'http://www.senjao.com/pay_callback.php',
            'finishUrl' => 'http://www.senjao.com/pay_finish.php',
            'desc' => '测试',
        );
        $plainText = json_encode($param);
        return $plainText;
    }

    public function getSignData( $str ){
        $private_key_yunlu = file_get_contents("rsa_private_key_yunlu.pem");
        $pi_key = openssl_pkey_get_private($private_key_yunlu);

        //签名
        openssl_sign($str, $signMsg, $pi_key,OPENSSL_ALGO_SHA1); //注册生成加密信息
        $signData = base64_encode($signMsg); //base64转码加密信息
        return $signData;
    }

    public function getEncryptKey(){
        $public_key_yunlu = file_get_contents("rsa_public_key_yunlu.pem");
        $pu_key = openssl_pkey_get_public($public_key_yunlu);
        openssl_public_encrypt($this->keyStr,$encryptKey,$pu_key);
        $encryptKey = base64_encode($encryptKey);

        return $encryptKey;
    }

    public function curlPost( $post_data ){
        $o = "";
        foreach ( $post_data as $k => $v ) {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        $headers = array();

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);

        return $output;
    }
}

$pay = new Pay();
$pay->toPay();